import {
  Controller,
  Get,
  Post,
  Body,
  Param,
  Put,
  Delete,
  Req,
  // NotFoundException,
} from '@nestjs/common';
import { Request, Response } from 'express';
import { User, Prisma } from '@prisma/client';
import { UsersService } from './user.service';
import { BusinessException } from '@/common/utils';

@Controller('users')
export class UsersController {
  constructor(private readonly usersService: UsersService) {}

  @Get()
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  async findAll(@Req() request: Request): Promise<User[]> {
    // console.log(request.user);
    return this.usersService.findAll();
  }

  @Get(':id')
  async findOne(
    @Param('id') id: string,
    // @Req() request: Request,
  ): Promise<User | null> {
    const info = await this.usersService.findOne(+id);
    if (!info) {
      // throw new NotFoundException('用户不存在');
      throw new BusinessException('用户不存在', 10002);
    }
    return info;
  }

  @Post()
  async create(@Body() data: Prisma.UserCreateInput): Promise<User> {
    return this.usersService.create(data);
  }

  @Put(':id')
  async update(
    @Param('id') id: string,
    @Body() data: Prisma.UserUpdateInput,
  ): Promise<User> {
    return this.usersService.update(+id, data);
  }

  @Delete(':id')
  async remove(@Param('id') id: string): Promise<User> {
    return this.usersService.remove(+id);
  }
}
